package com.omo.domain.machine.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.omo.domain.common.excel.service.IExportExcelService;
import com.omo.domain.machine.excel.EEMachineProductionBody;
import com.omo.domain.machine.excel.EEMachineProductionTitle;
import com.omo.domain.machine.excel.MachineProductionExportExcelInfo;
import com.omo.domain.machine.model.entity.MachineProductionEntity;
import com.omo.domain.machine.repository.IMachineProductionRepository;
import com.omo.domain.machine.service.IMachineProductionService;
import com.omo.types.excel.BaseExportExcelDataBody;
import com.omo.types.excel.BaseExportExcelInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class MachineProductionExportExcelServiceImpl implements IMachineProductionService {
    @Resource
    private IMachineProductionRepository repository;
    @Resource
    private MachineProductionExportExcelInfo machineProductionExportExcel;
    @Resource
    private IExportExcelService exportExcelService;

    @Override
    public void exportMachineProductionExcel(String machineName) {
        List<MachineProductionEntity> machineProductionEntityList = repository.queryMachineProduction(machineName);
        Set<String> versionSets = machineProductionEntityList.stream().map(MachineProductionEntity::getVersion).sorted().collect(Collectors.toSet());
        if (ObjectUtil.isNotEmpty(versionSets) && versionSets.size() > 1){
            machineProductionExportExcel.setExpand(true);
        }
        EEMachineProductionTitle eeMachineProductionTitle = machineProductionExportExcel.builderTitle(versionSets);
        List<BaseExportExcelDataBody> eeMachineProductionBodies = machineProductionExportExcel.builderBody(machineProductionEntityList);

        MachineProductionExportExcelInfo.ExportExcelMachineProductionEntity exportExcelMachineProductionEntity = new MachineProductionExportExcelInfo.ExportExcelMachineProductionEntity();
        exportExcelMachineProductionEntity.setTitle(eeMachineProductionTitle);
        exportExcelMachineProductionEntity.setBody(eeMachineProductionBodies);

        BaseExportExcelInfo.BaseExportExcelEntity<MachineProductionExportExcelInfo.ExportExcelMachineProductionEntity> machineProductionExportExcelEntity = machineProductionExportExcel.buildExportExcelEntity(
                exportExcelMachineProductionEntity, versionSets, machineName);
        exportExcelService.exportExcel(machineProductionExportExcelEntity);
    }
}
